package com.aoxin.controller.opm;

import java.util.List;
import java.util.Map;

import com.aoxin.util.enumerate.OperateType;
import com.aoxin.util.log.LogInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aoxin.model.opm.SlfRole;
import com.aoxin.service.inter.opm.RoleService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.Const;

/**
 * 角色Controller
 * 
 * @author Melody
 * @date 2018年3月21日 上午11:09:33
 */
@Controller
@ResponseBody
@RequestMapping("/web/role")
public class RoleController {

	@Autowired
	private RoleService roleService;

	/**
	 * 添加角色
	 * 
	 * @param role
	 *            角色
	 */
	@RequestMapping("/insertRole")
	@LogInfo(content = "角色",operateType = OperateType.INSERT)
	public AjaxRes insertRole(SlfRole role, AjaxRes ajaxRes) {
		try {
			this.roleService.insertSlfRole(role);
			ajaxRes.setSucceedMsg(Const.SAVE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 批量删除
	 * 
	 * @param roleIds
	 *            角色ID集合
	 */
	@RequestMapping("/deleteRoles")
	@LogInfo(content = "角色",operateType = OperateType.DELETE)
	public AjaxRes deleteRoles(@RequestParam("roleIds") List<Integer> roleIds, AjaxRes ajaxRes) {
		try {
			this.roleService.deleteSlfRoleByIds(roleIds);
			ajaxRes.setSucceedMsg(Const.DEL_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 根据角色ID查询角色
	 * 
	 * @param roleId
	 *            角色ID
	 * @return 角色
	 */
	@RequestMapping("/getRole")
	@LogInfo(content = "角色",operateType = OperateType.QUERY)
	public AjaxRes getRole(@RequestParam("roleId") Integer roleId, AjaxRes ajaxRes) {
		try {
			SlfRole slfRole = this.roleService.getSlfRole(roleId);
			ajaxRes.setSucceed(slfRole, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 修改角色
	 * 
	 * @param roleIds
	 *            角色ID集合
	 */
	@RequestMapping("/updateRole")
	@LogInfo(content = "角色",operateType = OperateType.UPDATE)
	public AjaxRes updateRole(SlfRole role, AjaxRes ajaxRes) {
		try {
			this.roleService.updateSlfRole(role);
			ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 修改角色状态
	 * 
	 * @param roleIds
	 *            角色ID集合
	 */
	@RequestMapping("/updateRoleStates")
	@LogInfo(content = "角色",operateType = OperateType.UPDATE)
	public AjaxRes updateRoleStates(@RequestParam("roleIds") List<Integer> roleIds, @RequestParam("state") Integer state, AjaxRes ajaxRes) {
		try {
			this.roleService.updateSlfRoleStates(roleIds, state);
			ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 查询角色
	 * 
	 * @param map
	 *            查询条件
	 * @return 角色集合
	 */
	@RequestMapping("/getRoleList")
	@LogInfo(content = "角色",operateType = OperateType.QUERY)
	public AjaxRes getRoleList(@RequestParam Map<String, Object> map, AjaxRes ajaxRes) {
		try {
			List<SlfRole> slfRoles = this.roleService.getSlfRoleList(map);
			ajaxRes.setSucceed(slfRoles, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 分页查询角色
	 * 
	 * @param map
	 *            条件集合
	 * @return 分页对象
	 */
	@RequestMapping("/slicedQueryRole")
	@LogInfo(content = "角色",operateType = OperateType.QUERY)
	public AjaxRes slicedQueryRole( @RequestParam Map<String, Object> map, AjaxRes ajaxRes) {
		try {
			List<SlfRole> slfRoles = this.roleService.getSlfRoleList(map);
			ajaxRes.setSucceed(slfRoles, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 获取下一排序号
	 * 
	 * @return 排序号
	 */
	@RequestMapping("/getNextSequence")
	@LogInfo(content = "角色",operateType = OperateType.QUERY)
	public AjaxRes getNextSequence(AjaxRes ajaxRes) {
		try {
			Long sequence = this.roleService.getNextSequence();
			ajaxRes.setSucceed(sequence, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	@RequestMapping("/getRoleName")
	@LogInfo(content = "角色名称",operateType = OperateType.QUERY)
	public AjaxRes getRoleName(AjaxRes ajaxRes){
		try {
			List<String> roleName = this.roleService.getRoleName();
			ajaxRes.setSucceed(roleName, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
}
